import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class EXA10_4 {
    public static void main(String[] args) {
        // 使用FutureTask和Callable创建Thread类对象的target目标对象
        FutureTask<String> ft = new FutureTask<>(new Callable<String>() {
            public String call() throws InterruptedException {
                int i;
                for (i = 0; i < 5; i++) {
                    System.out.println(Thread.currentThread().getName() + ": " + i);
                }
                return "线程中当循环结束时i变量的值 " + i; // 有返回值，类型要一致
            }
        });

        new Thread(ft, "Callable和FutureTask创建的线程").start(); // 创建线程类对象并运行

        try {
            System.out.println(ft.get()); // 获取线程结束时的返回值
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}